package com.market.controller.admin;

import com.market.bean.MarketAd;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.BaseRespVo;
import com.market.bean.common.CommonData;
import com.market.service.admin.AdminAdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author fan
 * @Description: 处理推广管理模块的广告管理的url
 * @since 2022/12/28 17:26
 */
@RestController
@RequestMapping(value = "/admin/ad")
public class AdminAdController {

    @Autowired
    AdminAdService adService;

    /**
     * 广告显示的列表
     *
     * @param pageInfo page 页码/limit 页条数/sort 已什么内容排序/order 正序或倒序
     * @param name     广告标题
     * @param content  广告内容
     * @return
     */
    @RequiresPermissions("admin:ad:list")
    @GetMapping("list")
    public BaseRespVo<CommonData> list(BasePageInfo pageInfo, String name, String content) {
        CommonData<MarketAd> data = adService.list(pageInfo, name, content);
        return BaseRespVo.ok(data);
    }

    /**
     * 增加广告
     *
     * @param marketAd 广告的信息
     * @return
     */
    @RequiresPermissions("admin:ad:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketAd marketAd) {
        MarketAd data = adService.create(marketAd);
        if (data.getId() == null) {
            return BaseRespVo.invalidData("添加失败");
        }
        return BaseRespVo.ok(data);
    }

    /**
     * 更新广告
     *
     * @param marketAd 广告的信息
     * @return
     */
    @RequiresPermissions("admin:ad:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketAd marketAd) {
        MarketAd data = adService.update(marketAd);
        return BaseRespVo.ok(data);
    }

    /**
     * 删除广告
     *
     * @param marketAd 广告的信息
     * @return
     */@RequiresPermissions("admin:ad:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketAd marketAd) {
        adService.delete(marketAd);
        return BaseRespVo.ok();
    }

}
